<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.io.* "%>
<%@ page import="jakarta.servlet.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
	String userid=request.getParameter("id");//取得传递过来的用户参数，用于查询和删除
	/*System.out.println("userid:"+userid);
	System.out.println("car_name:"+request.getParameter("car_name"));
	System.out.println("car_type:"+request.getParameter("car_type"));
	*/
	Connection conn=null;
	try{
		Class.forName("org.h2.Driver");//加载驱动
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
		return;
	}
	try {
		conn = DriverManager.getConnection("jdbc:h2:d:/CompanyCar/temp/mydbtest", "sa", "");
		String sql="update car_info set car_name=?,car_type=?,license_plate=?,displacement=?,start_date=?,management_department=?,responsible_person=?,register_person=?,asset_number=?,purchase_date=?,value=?,frame_number=?,car_usage=?,usage_status=?,usage_department=?,usage_person=?,entry_date=? where car_id='"+userid+"'";
		PreparedStatement pstmt  = conn.prepareStatement(sql);
		pstmt.setString(1,request.getParameter("car_name"));
		pstmt.setString(2,request.getParameter("car_type"));
		pstmt.setString(3,request.getParameter("license_plate"));
		System.out.println(request.getParameter("displacement"));
		pstmt.setFloat(4,Float.parseFloat(request.getParameter("displacement")));
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date date=null;
		String dateString=request.getParameter("start_date");
		date= dateFormat.parse(dateString);
		java.sql.Date start_date = new java.sql.Date(date.getTime());
		pstmt.setDate(5,start_date);
		pstmt.setString(6,request.getParameter("management_department"));
		pstmt.setString(7,request.getParameter("responsible_person"));
		pstmt.setString(8,request.getParameter("register_person"));
		pstmt.setString(9,request.getParameter("asset_number"));
		dateString = request.getParameter("purchase_date");
		date = dateFormat.parse(dateString);
		java.sql.Date purchase_date = new java.sql.Date(date.getTime());
		pstmt.setDate(10,purchase_date);
		pstmt.setFloat(11,Float.parseFloat(request.getParameter("value")));
		pstmt.setString(12,request.getParameter("frame_number"));
		pstmt.setString(13,request.getParameter("car_usage"));
		pstmt.setString(14,request.getParameter("usage_status"));
		pstmt.setString(15,request.getParameter("usage_department"));
		pstmt.setString(16,request.getParameter("usage_person"));
		dateString = request.getParameter("entry_date");
		date = dateFormat.parse(dateString);
		java.sql.Date entry_date = new java.sql.Date(date.getTime());
		pstmt.setDate(17,entry_date);
		/*
		Part part = request.getPart("data_attachment");
		if (part != null && part.getSize() > 0) {
			InputStream inputStream = part.getInputStream();
			byte[] bytes = inputStream.readAllBytes();
			Blob data = conn.createBlob();
			data.setBytes(1, bytes);//createBlob()方法需要传入一个InputStream类型的参数，而不是byte[]类型的参数。您可以使用setBytes()方法将字节数组存储到Blob对象中，然后再将Blob对象存储到数据库中。
			pstmt.setBlob(18,data);
		}*/
		System.out.println(sql);
		//执行数据库插入，获取结果集
		int result=pstmt.executeUpdate();
		System.out.println(result);
		//关闭表达式对象
		pstmt.close();			
		//关闭数据库				
		conn.close();
	} catch (SQLException e) {
		e.printStackTrace();
	} finally{
	//防止数据库未正常关闭
	try{
		if (conn!=null && (!conn.isClosed())){
			conn.close();
		}
	}catch(SQLException e){
		e.printStackTrace();
	}
}
	response.sendRedirect("C-Car_Admin.jsp");
%>


</body>
</html>